<template>
  <div class="mod-distribution-prod">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      v-model="dataForm"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
      @selection-change="selectionChange"
    >
      <template slot-scope="scope" slot="prodName">{{scope.row.product.prodName}}</template>

      <template slot-scope="scope" slot="pic">
        <img :src="scope.row.product.pic" width="100" height="100" />
      </template>

      <!-- <template slot-scope="scope"
                slot="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click.stop="addOrUpdateHandle(scope.row)"
                   v-if="isAuth('distribution:distributionProd:save')">新增</el-button>

        <el-button type="danger"
                   size="small"
                   @click.stop="deleteHandle"
                   :disabled="dataListSelections.length <= 0"
                   v-if="isAuth('distribution:distributionProd:delete')">批量删除</el-button>
      </template>-->

      <template slot-scope="scope" slot="state">
        <el-tag v-if="scope.row.state === 0" size="small">{{$t('prodList.ShopkeeperOffShelf')}}</el-tag>
        <el-tag
          v-if="scope.row.state === 1"
          size="small"
          type="success"
        >{{$t('publics.metUpperShelf')}}</el-tag>
        <el-tag
          v-else-if="scope.row.state ===2"
          size="small"
          type="danger"
        >{{$t('groups.offlineViolation')}}</el-tag>
        <el-tag
          v-else-if="scope.row.state ===3"
          size="small"
          type="warning"
        >{{$t('product.pendingReview')}}</el-tag>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(scope.row)"
          v-if="isAuth('platform:distributionProd:info')"
        >{{$t('coupon.viewDetails')}}</el-button>
        <el-button
          icon="el-icon-bottom"
          size="small"
          type="primary"
          v-if="isAuth('platform:distributionProd:update') && scope.row.state<2"
          @click="offlineDistributionProdHandle(scope.row)"
        >{{$t('distribution.offlineDistributionProd')}}</el-button>
        <el-button
          icon="el-icon-document-checked"
          size="small"
          type="primary"
          v-if="isAuth('platform:distributionProd:update') && scope.row.state > 1"
          @click="distributionProdAuditHandle(scope.row.distributionProdId)"
        >{{$t('distributionProdLog.offlineManagement')}}</el-button>

        <!-- <el-button type="danger"
                   icon="el-icon-delete"
                   size="small"
                   v-if="isAuth('prod:prod:delete')"
        @click="deleteHandle(scope.row)">删除</el-button>-->
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 商品审核弹窗 -->
    <DistributionProdAudit
      v-if="distributionProdAuditVisible"
      ref="distributionProdAudit"
      selectUrl="/platform/distributionProd/getOfflineEventByDistProdId"
      updateUrl="/platform/distributionProd/auditDistributionProd"
      @refreshDataList="refreshChange"
    ></DistributionProdAudit>
  </div>
</template>

<script>
import { tableOption } from '@/crud/distribution/distributionProd'
import AddOrUpdate from './distribution-prod-add-or-update'
import DistributionProdAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataForm: {
        prodName: ''
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      searchForm: {},
      dataList: [],
      tableOption: tableOption,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      distributionProdAuditVisible: false
    }
  },
  components: {
    AddOrUpdate,
    DistributionProdAudit
  },
  methods: {
    // 多选回调
    selectionChange (list) {
      this.dataListSelections = list
    },
    // 新增 / 修改
    addOrUpdateHandle (data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data)
      })
    },
    // 点击查询
    searchChange (params) {
      this.page.currentPage = 1
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 刷新回调
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/distributionProd/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page ? page.currentPage : 1,
          size: page ? page.pageSize : 20
        }, this.searchForm))
      }).then(({ data }) => {
        this.page.total = data.total
        this.page.pageSize = data.size
        this.page.currentPage = data.current
        this.dataList = data.records
        this.dataListLoading = false
      })
    },
    // 删除
    deleteHandle (row, index) {
      var ids = row.distributionProdId ? [row.distributionProdId] : this.dataListSelections.map(item => {
        return item.distributionProdId
      })
      // `确定进行[${row.distributionProdId ? '删除' : '批量删除'}]操作?`
      var tip = this.$i18n.t('remindPop.makeSure') + '[' + row.distributionProdId ? this.$i18n.t('remindPop.delete') : this.$i18n.t('remindPop.batchDeletion')
      tip = tip + ']' + this.$i18n.t('remindPop.operation') + '?'
      this.$confirm(tip, this.$i18n.t('remindPop.remind'), {
        confirmButtonText: this.$i18n.t('remindPop.confirm'),
        cancelButtonText: this.$i18n.t('remindPop.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/platform/distributionProd'),
          method: 'delete',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('remindPop.success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      })
    },
    // 下线活动
    offlineDistributionProdHandle (row) {
      console.log(row)
      // '确定对活动需要修改[' + row.couponName + ']进行下线操作？如是，请填写下线理由'
      var tips = this.$i18n.t('groups.removeActivityNameTips')
      tips = tips.replace('ACTIVITYNAME', row.product.prodName)
      this.$prompt(tips, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel')
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error(this.$i18n.t('seckill.offlineReasonTip'))
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/distributionProd/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.distributionProdId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    distributionProdAuditHandle (id) {
      this.distributionProdAuditVisible = true
      this.$nextTick(() => {
        this.$refs.distributionProdAudit.init(id)
      })
    }
  }
}
</script>
<style scoped>
div /deep/ .avue-crud__menu{
    overflow:inherit;
    min-height: 0;
}
div /deep/ .avue-crud__right{
    position: absolute;
    right: 0;
    top: -58px;
}
div /deep/ .avue-crud__pagination{
  margin-bottom: 25px;
}
</style>